0: Preparation

Defining the input and output files

# Clean the working environment
rm(list = ls())

knitr::opts_chunk$set(echo = TRUE)

empirical_species <- "German Shepherd"
document_title <- paste(empirical_species," Pipeline Results")

MAF_pruning_used <- FALSE

N_e <- 50
document_sub <- paste("MAF 0.05 used, but only for H_E-computation, N_e=", N_e)




# if (MAF_pruning_used == FALSE) {
#   document_sub <- paste("No MAF-based pruning used, N_e =", N_e)
#   
#   
# } else {
#   document_sub <- paste("MAF-based pruning used, N_e =", N_e)
# }

############################################ 
# Parameters used for displaying the result
############################################ 
ROH_frequency_decimals <- 1
H_e_values_decimals <- 5
F_ROH_values_decimals <- 3





####################################  
# Defining the input files
#################################### 

Selection_strength_test_dir <- Sys.getenv("Selection_strength_test_dir")

Sweep_test_dir <-  Sys.getenv("Sweep_test_dir")

############### 
## Empirical ###
###############

### ROH hotspots ###
Empirical_data_ROH_hotspots_dir  <- Sys.getenv("Empirical_data_ROH_hotspots_dir")
Empirical_data_autosome_ROH_freq_dir <- Sys.getenv("Empirical_data_autosome_ROH_freq_dir")
### Inbreeding coefficient ###

Empirical_data_F_ROH_dir  <- Sys.getenv("Empirical_data_F_ROH_dir")

### Expected Heterozygosity distribution ###
Empirical_data_H_e_dir <- Sys.getenv("Empirical_data_H_e_dir")

############### 
## Simulated ###
###############

### Causative variant ###
variant_freq_plots_dir <- Sys.getenv("variant_freq_plots_dir")
variant_position_dir <- Sys.getenv("variant_position_dir")
pruned_replicates_count_dir <- Sys.getenv("pruned_replicates_count_dir")

Selection_causative_variant_windows_dir <- Sys.getenv("Selection_causative_variant_windows_dir")
causative_variant_windows_H_e_dir <- Sys.getenv("causative_variant_windows_H_e_dir")

### ROH hotspots ###
Neutral_model_ROH_hotspots_dir <- Sys.getenv("Neutral_model_ROH_hotspots_dir")
Neutral_model_autosome_ROH_freq_dir <- Sys.getenv("Neutral_model_autosome_ROH_freq_dir")

Selection_model_ROH_hotspots_dir  <- Sys.getenv("Selection_model_ROH_hotspots_dir")
Selection_model_autosome_ROH_freq_dir <- Sys.getenv("Selection_model_autosome_ROH_freq_dir")

### Inbreeding coefficient ###
Neutral_model_F_ROH_dir  <- Sys.getenv("Neutral_model_F_ROH_dir")
Selection_model_F_ROH_dir  <- Sys.getenv("Selection_model_F_ROH_dir")

### Expected Heterozygosity distribution ###
Neutral_model_H_e_dir <- Sys.getenv("Neutral_model_H_e_dir")
Selection_model_H_e_dir <- Sys.getenv("Selection_model_H_e_dir")


histogram_line_sizes <- 3
empirical_data_color <- "darkgreen"
neutral_model_color <- "blue" 
selection_model_color <- "purple"

output_dir <- Sys.getenv("Pipeline_results_output_dir") 


# Sys.getenv()  

# # Set the working directory for notebook chunks
# knitr::opts_knit$set(root.dir = output_dir_sweep_test)

Loading libraries

library(knitr)
## Warning: package 'knitr' was built under R version 4.3.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1

Causative variant

Variant fixation

Fixation probability

Fixation time

# Function to determine the number of rows until fixation is reached
time_to_fixation <- function(data, threshold = 0.9) {
  # Find the index of the first row where the allele frequency is 0.9 or higher
  fixation_index <- which(data$allele_frequency >= threshold)[1]
  
  # Return the number of rows until fixation is reached
  return(fixation_index - 1)
}

Summary - Variant fixation

Selection_coefficient Fixation_probability Avg_Fixation_time Min_fixation_time Max_fixation_time
1 s=0.05 0.2 33.80 27 39
3 s=0.2 16.3 32.40 22 39
2 s=0.1 1.5 31.85 18 39
4 s=0.3 27.8 27.55 18 39
5 s=0.4 40.8 20.95 15 29
6 s=0.5 40.0 16.65 10 26
7 s=0.6 35.7 11.45 8 16
8 s=0.7 46.5 10.25 7 13
9 s=0.8 45.5 7.45 4 10

Causative variant windows

Variant position

Window lengths

Causative variant windows

Summary - Causative variant windows

Selection_coefficient Avg_Length_Mb Avg_window_freq Min_freq Max_freq Avg_freq_variant_100k_window
1 s=0.05 3.655 71.0 32 100 71.9
3 s=0.2 4.430 78.9 34 100 79.3
4 s=0.3 4.435 83.1 38 100 84.0
5 s=0.4 4.625 78.1 32 100 78.2
2 s=0.1 4.775 73.2 12 100 73.9
6 s=0.5 5.040 92.9 56 100 93.6
7 s=0.6 5.875 87.2 48 100 88.0
8 s=0.7 6.220 93.2 36 100 94.4
9 s=0.8 7.895 88.9 28 100 89.9

Standard Error Confidence interval function

Confidence level <=> konfidensgrad

# Function to calculate standard error and its confidence interval
standard_error_confidence_interval_fun <- function(observed_data, confidence_level = 0.95) {
  
  # Calculate standard error
  n <- length(observed_data)
  standard_deviation <- sd(observed_data)
  standard_error <- standard_deviation / sqrt(n - 1)
  
  # Calculate confidence interval based on standard error

  alpha <- (1 - confidence_level) / 2
  margin_of_error <- qnorm(1 - alpha) * standard_error
  mean_estimate <- mean(observed_data)
  # Calculate the percentiles for the confidence interval
  confidence_interval_lower_bound <- mean_estimate - margin_of_error # 2.5th percentile (2σ)
  confidence_interval_upper_bound <- mean_estimate + margin_of_error # 97.5th percentile (2σ)
  
  # Return confidence interval
  return(c(confidence_interval_lower_bound, confidence_interval_upper_bound))
}





# # Function to calculate bootstrap confidence intervals
# standard_error_confidence_interval_fun <- function(observed_data, n_bootstraps = 1000, confidence_level = 0.95) {
#   
#   # Function to calculate the mean for each bootstrap sample
#   compute_bootstrap_mean_fun <- function(observed_data_urn) {
#     bootstrap_dataset <- sample(observed_data_urn, replace = TRUE)
#     bootstrap_estimate <- mean(bootstrap_dataset)
#     return(bootstrap_estimate)
#   }
#   
#   # Perform bootstrap resampling
#   bootstrap_point_estimates <- replicate(n_bootstraps, compute_bootstrap_mean_fun(observed_data))
#   
#   # Calculate the percentiles for the confidence interval
#   alpha <- (1 - confidence_level) / 2
#   confidence_interval_lower_bound <- quantile(bootstrap_point_estimates, alpha) # 2.5th percentile
#   confidence_interval_upper_bound <- quantile(bootstrap_point_estimates, 1 - alpha) # 97.5th percentile
#   
#   # Return the confidence interval
#   return(c(confidence_interval_lower_bound, confidence_interval_upper_bound))
# }

1: ROH-Frequency

1.1 : Autosome ROH-frequencies

1.1.1 : Empirical - ROH frequency

1.1.2: Neutral Model - ROH frequency

1.1.3: Selection Model

1.1.4 Summary - ROH-hotspot threshold

## ROH-hotspot selection testing results://n
Model Avg_ROH_hotspot_threshold
Neutral 40.0
s=0.1 42.6
s=0.3 45.1
s=0.05 46.6
s=0.2 47.0
s=0.4 47.0
s=0.6 52.0
s=0.5 54.2
s=0.7 58.2
s=0.8 60.6
Empirical 75.0

1.2 ROH-hotspots - ROH Frequency and Length

2: Inbreeding coefficient

2.1 Empirical data

## Overall Average Avg_F_ROH for  german_shepherd : 0.2753012 //n

2.2 Neutral Model

## Point estimate F_ROH across all 20 simulations: 0.3231565 //n
## [1] "Bootstrap 95% Confidence Interval: [0.302707382298313, 0.343605637701687]"

2.3 Selection Model

## Point estimate F_ROH across all 20 simulations for  selection_model_s005_chr3 : 0.3789376 //n[1] "Bootstrap 95% Confidence Interval: [0.356936709259486, 0.400938510740514]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s01_chr3 : 0.3475749 //n[1] "Bootstrap 95% Confidence Interval: [0.329352061370301, 0.365797778629699]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s02_chr3 : 0.3852102 //n[1] "Bootstrap 95% Confidence Interval: [0.360506697169921, 0.409913642830079]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s03_chr3 : 0.382973 //n[1] "Bootstrap 95% Confidence Interval: [0.356057264547997, 0.409888755452003]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s04_chr3 : 0.4022861 //n[1] "Bootstrap 95% Confidence Interval: [0.379804687863133, 0.424767512136866]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s05_chr3 : 0.438943 //n[1] "Bootstrap 95% Confidence Interval: [0.407964226284795, 0.469921813715205]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s06_chr3 : 0.4301045 //n[1] "Bootstrap 95% Confidence Interval: [0.398231547108479, 0.461977472891521]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s07_chr3 : 0.4466082 //n[1] "Bootstrap 95% Confidence Interval: [0.419672277441459, 0.473544062558542]"

## Point estimate F_ROH across all 20 simulations for  selection_model_s08_chr3 : 0.4687358 //n[1] "Bootstrap 95% Confidence Interval: [0.432117907312471, 0.505353732687529]"

2.4 F_ROH summary

Model F_ROH Lower_CI Upper_CI
Empirical 0.275 NA NA
Neutral 0.323 0.303 0.344
s=0.1 0.348 0.329 0.366
s=0.05 0.379 0.357 0.401
s=0.3 0.383 0.356 0.410
s=0.2 0.385 0.361 0.410
s=0.4 0.402 0.380 0.425
s=0.6 0.430 0.398 0.462
s=0.5 0.439 0.408 0.470
s=0.7 0.447 0.420 0.474
s=0.8 0.469 0.432 0.505

2.4.1 Visualizaing F_ROH

## Models where empirical F_ROH is within CI:
## character(0)
## 
## Models where empirical F_ROH is outside CI:
##  [1] "s=0.05"  "s=0.1"   "s=0.2"   "s=0.3"   "s=0.4"   "s=0.5"   "s=0.6"  
##  [8] "s=0.7"   "s=0.8"   "Neutral"

3: Expected Heterozygosity

3.1 Empirical data

3.2 Neutral Model

3.3 Selection Model

## Uncommented because change of analysis

3.4 Causative Variant Window

## Point estimate H_e across all 20 simulations for  s005_chr3 : 0.264204 //n[1] "Bootstrap 95% Confidence Interval: [0.216383523904835, 0.31202456524621]"
## Point estimate H_e across all 20 simulations for  s01_chr3 : 0.2289063 //n[1] "Bootstrap 95% Confidence Interval: [0.186100305140173, 0.27171234697082]"
## Point estimate H_e across all 20 simulations for  s02_chr3 : 0.2102098 //n[1] "Bootstrap 95% Confidence Interval: [0.180080379412311, 0.240339211230964]"
## Point estimate H_e across all 20 simulations for  s03_chr3 : 0.2372416 //n[1] "Bootstrap 95% Confidence Interval: [0.18072998081691, 0.293753291910363]"
## Point estimate H_e across all 20 simulations for  s04_chr3 : 0.2983005 //n[1] "Bootstrap 95% Confidence Interval: [0.25297553388615, 0.343625395707606]"
## Point estimate H_e across all 20 simulations for  s05_chr3 : 0.1885108 //n[1] "Bootstrap 95% Confidence Interval: [0.133608817455569, 0.243412849211097]"
## Point estimate H_e across all 20 simulations for  s06_chr3 : 0.2728916 //n[1] "Bootstrap 95% Confidence Interval: [0.21702383329794, 0.328759446649333]"
## Point estimate H_e across all 20 simulations for  s07_chr3 : 0.2464954 //n[1] "Bootstrap 95% Confidence Interval: [0.192100825664963, 0.300889976125838]"
## Point estimate H_e across all 20 simulations for  s08_chr3 : 0.254731 //n[1] "Bootstrap 95% Confidence Interval: [0.204290957496677, 0.305171039070829]"

3.4 Genomewide 5th percentile comparison - Expected Heterozygosity Summary

Model H_e_5th_percentile Lower_CI Upper_CI
s02_chr3 0.12776 0.11884 0.13668
s07_chr3 0.13370 0.12378 0.14362
s05_chr3 0.13510 0.12690 0.14330
s08_chr3 0.13572 0.12556 0.14589
s005_chr3 0.13601 0.12648 0.14553
Neutral 0.13741 0.12666 0.14817
s01_chr3 0.13828 0.12896 0.14760
s06_chr3 0.14133 0.12831 0.15435
s04_chr3 0.14190 0.12903 0.15477
s03_chr3 0.14430 0.13057 0.15803
Empirical NA NA NA

4: Summary

4.0 General comparison

4.0.1 ROH-hotspot threshold comparison

## 
##  ROH-hotspot threshold comparison between the different datasets
Model Avg_ROH_hotspot_threshold
Neutral 40.0
s=0.1 42.6
s=0.3 45.1
s=0.05 46.6
s=0.2 47.0
s=0.4 47.0
s=0.6 52.0
s=0.5 54.2
s=0.7 58.2
s=0.8 60.6
Empirical 75.0

4.0.2 F_ROH comparison

Model F_ROH Lower_CI Upper_CI
Empirical 0.275 NA NA
Neutral 0.323 0.303 0.344
s=0.1 0.348 0.329 0.366
s=0.05 0.379 0.357 0.401
s=0.3 0.383 0.356 0.410
s=0.2 0.385 0.361 0.410
s=0.4 0.402 0.380 0.425
s=0.6 0.430 0.398 0.462
s=0.5 0.439 0.408 0.470
s=0.7 0.447 0.420 0.474
s=0.8 0.469 0.432 0.505

4.1: Hotspot comparison

4.1.1: Selection test (Sweep test)

## [1] "Selection test results"
## [1] "ROH-hotspot windows with an mean H_e Value lower or equal to the lower confidence interval of the fifth percentile of the neutral model are classified as being under selection"
## [1] "5th percentile of the neutral model is: 0.1266621"
Name Under_selection Window_based_Average_H_e
Hotspot_chr3_window_1 No 0.12799
Hotspot_chr3_window_3 No 0.13260
Hotspot_chr17_window_2 No 0.14866
Hotspot_chr3_window_2 No 0.15005
Hotspot_chr17_window_1 No 0.15685
Hotspot_chr19_window_1 No 0.17061
## [1] "ROH-hotspots under selection:"
Name Under_selection Window_based_Average_H_e

4.1.2: Selection Strength Test (Sweep test)

4.1.1.1 Extracting Causative windows under selection

Model H_e Lower_CI Upper_CI Under_Selection
Neutral 0.13741 0.12666 0.14817 No
s=0.5 0.18851 0.13361 0.24341 No
s=0.2 0.21021 0.18008 0.24034 No
s=0.1 0.22891 0.18610 0.27171 No
s=0.3 0.23724 0.18073 0.29375 No
s=0.7 0.24650 0.19210 0.30089 No
s=0.8 0.25473 0.20429 0.30517 No
s=0.05 0.26420 0.21638 0.31202 No
s=0.6 0.27289 0.21702 0.32876 No
s=0.4 0.29830 0.25298 0.34363 No

4.1.1.1 Extracting Hotspots under selection

*** Behöver bootstrap av 5th percentiles från neutral model

Hotspot - Causative Window - Comparison

Model Lengths_Mb Avg_ROH_Freq
Hotspot_chr3_window_1 10.900 81.3
s=0.8 7.895 88.9
s=0.7 6.220 93.2
s=0.6 5.875 87.2
s=0.5 5.040 92.9
s=0.1 4.775 73.2
s=0.4 4.625 78.1
s=0.3 4.435 83.1
s=0.2 4.430 78.9
Hotspot_chr19_window_1 4.400 75.6
s=0.05 3.655 71.0
Hotspot_chr3_window_2 3.200 76.3
Hotspot_chr3_window_3 2.700 77.6
Hotspot_chr17_window_1 2.000 76.4
Hotspot_chr17_window_2 0.600 76.1

Visualizing and scaling

5 Visualizing Expected Heterozygoisty

5.1 Bootstrap CI for mean 5th percentile H_e

## Models where empirical H_e is within CI for Hotspot_chr3_window_1 :
## character(0)
## 
## Models where empirical H_e is outside CI for Hotspot_chr3_window_1 :
## character(0)

## Models where empirical H_e is within CI for Hotspot_chr3_window_3 :
## character(0)
## 
## Models where empirical H_e is outside CI for Hotspot_chr3_window_3 :
## character(0)

## Models where empirical H_e is within CI for Hotspot_chr17_window_2 :
## character(0)
## 
## Models where empirical H_e is outside CI for Hotspot_chr17_window_2 :
## character(0)

## Models where empirical H_e is within CI for Hotspot_chr3_window_2 :
## character(0)
## 
## Models where empirical H_e is outside CI for Hotspot_chr3_window_2 :
## character(0)

## Models where empirical H_e is within CI for Hotspot_chr17_window_1 :
## character(0)
## 
## Models where empirical H_e is outside CI for Hotspot_chr17_window_1 :
## character(0)

## Models where empirical H_e is within CI for Hotspot_chr19_window_1 :
## character(0)
## 
## Models where empirical H_e is outside CI for Hotspot_chr19_window_1 :
## character(0)

5.2 5th percentile of the extreme H_e values